<?php 
$cs = Yii::app()->clientScript;

$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'c_next_objectives_tree'
	),
	'ui' => array (
		'theme_name' => 'checkbox'
	),
	'options' => array (
		'plugins' => array (
			'checkbox' => array (
				'three_state' => 'false',
			)
		)
	),
	'genDiv' => false,
	'callback' => array (
		'beforechange' => "js:function(NODE) { return false; }",
	)
));

$form_name = 'TopicObjectiveForm[objectives]';
?>

<div class="yiiForm" id="next_objectives">
	<div id="c_next_objectives_tree" class="topic_tree">
	<?php

displayObjectiveSteps($this, $form_name, $topicObjectiveForm->steps);
?>
	</div><!-- yiiForm -->
</div><!-- yiiForm -->
 
<?php

function displayObjectiveSteps($p, $topicObjectiveForm, $steps) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul>";
		foreach ($steps as $step) {
			if ($step->canBeObjective()) {
				echo "<li id='c_node_" . $step->id . "' class='open node'>\n";
				echo "\t<a href='#'>" . $step->label . "</a>";
				displayObjectiveTopics($p, $topicObjectiveForm, $step->topics);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

function displayObjectiveTopics($p, $topicObjectiveForm, $topics) {
	$image = "";

	if (is_array($topics) && count($topics) > 0) {
		echo "<ul>";
		foreach ($topics as $topic) {
			if ($topic->canBeObjective()) {
				echo "<li id='c_node_" . $topic->project_topic_id . "' class='node'>\n\t<a href='#'><ins>&nbsp;</ins>";
				if ($topic->topic->objective == Constants::TRUE){
					echo "</a> " . CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
						'class' => 'toogle_layer',
						'id' => 'c_img_comment_' . $topic->project_topic_id
					)) . " <a href='#'>";
				}
				echo $topic->label . "</a>";
				$p->widget('adviser.widgets.ObjectiveLayer', array (
					'topic_id' => $topic->project_topic_id,
					'columns' => 60,
					'rows' => 5,
					'prefix' => 'c_',
					'objective' => $topic->topic->objective,
					'objective_topic' => $topic->topic->objective_topic,
					'objective_needs' => $topic->topic->objective_needs,
					'objective_contacts' => $topic->topic->objective_contacts,
					'form_name' => $topicObjectiveForm,
					'read_only' => true
				));

				displayObjectiveTopics($p, $topicObjectiveForm, $topic->children);
				echo "</li>";
			}
		}
		echo "</ul>";
	}
}

$script =<<<JS
$("div[id^='c_objective_layer_']").hide();
$("#c_next_objectives_tree").parent().append('<hr class="separation" />');
init_objectives($('#c_next_objectives_tree'), 'c_', false);
$("#c_next_objectives_tree input[id^='c_objective_']")
  .not("input[id^='c_objective_topic_'],input[value='0']")
  .parent().parent()
  .each(function(index)
    {
      var nodeId = this.id.replace('c_node_', '')
      jQuery.tree.plugins.checkbox.check(this);
      var topic = $("#c_objective_topic_"+nodeId).val();
      if (topic == "" || topic === null || topic === undefined)
      {
        $("#c_img_comment_"+nodeId).click();
      }
    });
JS;
$cs->registerScript('c_topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_objective.js', CClientScript::POS_HEAD);

?>
